﻿using System.Windows;
using Microsoft.Practices.Prism.MefExtensions;
using System.ComponentModel.Composition.Hosting;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Logging;
using Infrastructure.Behaviour;
using Infrastructure.Utility;

namespace MainClientSystem
{
    public class TheBootstrapper : MefBootstrapper
    {
        protected override void InitializeShell()
        {
            base.InitializeShell();

            Application.Current.MainWindow = (Shell)this.Shell;
            Application.Current.MainWindow.Show();
        }

        protected override DependencyObject CreateShell()
        {
            return Container.GetExportedValue<Shell>();
        }

        protected override void ConfigureAggregateCatalog()
        {
            base.ConfigureAggregateCatalog();

            // Prism's AggregateCatalog is a catalog of all MEF composable parts
            // within the application.
            // We add the parts corresponding to the current assembly to it
            this.AggregateCatalog.Catalogs.Add( new AssemblyCatalog( this.GetType().Assembly ) );

            //DirectoryCatalog catalog = new DirectoryCatalog( "..\\..\\DirectoryModules" );
            //this.AggregateCatalog.Catalogs.Add( catalog );

            //2012/10/05
            this.AggregateCatalog.Catalogs.Add( new AssemblyCatalog( typeof( AutoPopulateExportedViewsBehavior ).Assembly ) );
        }

        protected override IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
        {
            var factory = base.ConfigureDefaultRegionBehaviors();

            // Behavior that registers all views decorated with the ViewExport attribute
            factory.AddIfMissing( "AutoPopulateExportedViewsBehavior", typeof( AutoPopulateExportedViewsBehavior ) );

            return factory;
        }

        protected override IModuleCatalog CreateModuleCatalog()
        {
            // When using MEF, the existing Prism ModuleCatalog is still the place to configure modules via configuration files.
            return new ConfigurationModuleCatalog();
        }

        protected override ILoggerFacade CreateLogger()
        {
            return new XmlLogger();
        }
    }
}
